package com.teckteam.ptvsystem.view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

/**
 * Loading dialog box that will provide user visual feedback on any
 * progress that requires them to wait.
 * 
 */

public class LoadingDialog extends JDialog implements Observer
{
   private JLabel          message, task;
   private JPanel          panel;
   private JProgressBar    progressBar;
   private static LoadingDialog
      instance = null;
   
   protected LoadingDialog()
   {
      setSize(400, 70);
      setResizable(false);
      //setTitle("Loading...");
      //setModal(true);
      setUndecorated(true);
      setLocationRelativeTo(null);
      setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
      panel = new JPanel();
      progressBar = new JProgressBar();
      message = new JLabel("Please wait while processing...");
      task = new JLabel("");
      progressBar.setPreferredSize(new Dimension(385, 20));
      
      panel.add(message);
      panel.add(progressBar);
      panel.add(task);
      this.getContentPane().add(panel);
   }
   
   public static LoadingDialog getInstance() {
      if (instance == null) {
         instance = new LoadingDialog();
      }
      return instance;
   }
   
   public void setLoading(int max)
   {
      setLocationRelativeTo(null);
      //progressBar = new ProgressMonitor(this, null, null, 0, max);
      progressBar.setMaximum(max);
      progressBar.setStringPainted(true);
      progressBar.setValue(0);
      setVisible(true);
   }
   
   public void setProgress(int current, String task)
   {
      progressBar.setValue(current);
      progressBar.setString(task);
   }

   public void update(Observable o, Object arg)
   {
      progressBar.setValue(Integer.parseInt(arg.toString()));
   }
}
